package cn.tedu.luban.order.main.converter;

import cn.tedu.luban.order.protocol.VO.AdminOrderDetailVO;
import cn.tedu.luban.order.protocol.VO.AdminOrderInfoVo;
import cn.tedu.luban.order.protocol.VO.AdminOrderItemVO;
import cn.tedu.luban.order.protocol.VO.AdminRequestOrderInfoVO;
import cn.tedu.luban.order.protocol.VO.AdminUserInfoVO;
import cn.tedu.luban.order.protocol.VO.AdminWorkerInfoVO;
import cn.tedu.luban.order.protocol.VO.AttachInfoVO;
import cn.tedu.luban.order.protocol.VO.FrontOrderDetailVO;
import cn.tedu.luban.order.protocol.VO.FrontOrderInfoVo;
import cn.tedu.luban.order.protocol.VO.FrontOrderItemVO;
import cn.tedu.luban.order.protocol.VO.FrontRequestOrderInfoVO;
import cn.tedu.luban.order.protocol.VO.FrontUserInfoVO;
import cn.tedu.luban.order.protocol.bo.AttachInfoBO;
import cn.tedu.luban.order.protocol.bo.OrderBO;
import cn.tedu.luban.order.protocol.dos.OrderDO;
import cn.tedu.luban.order.protocol.param.OrderCreateParam;
import cn.tedu.luban.order.protocol.param.OrderParam;
import cn.tedu.luban.order.protocol.query.AdminOrderDBPagerQuery;
import cn.tedu.luban.order.protocol.query.AdminOrderListQuery;
import cn.tedu.luban.order.protocol.query.FrontOrderDBPagerQuery;
import cn.tedu.luban.order.protocol.query.FrontOrderListQuery;
import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.pager.PagerResult;
import com.tedu.inn.protocol.pager.SimplePagerQuery;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import springfox.documentation.spi.service.contexts.SecurityContext;

@Component
public class OrderConverter {
    public PagerResult<AdminOrderItemVO> bo2PagerResultVO(ListRecordTotalBO<OrderBO> orderListTotalRecord,
        SimplePagerQuery orderQuery) {
        List<AdminOrderItemVO> adminOrderVOList = this.bos2vos(orderListTotalRecord.getList());
        PagerResult<AdminOrderItemVO> pagerResult = new PagerResult<>(orderQuery);
        pagerResult.setObjects(adminOrderVOList);
        pagerResult.setTotal(orderListTotalRecord.getTotal());
        return pagerResult;
    }
    public List<AdminOrderItemVO> bos2vos(List<OrderBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AdminOrderItemVO> adminOrderVOList = new ArrayList<>(list.size());
        for (OrderBO orderBo : list) {
            AdminOrderItemVO adminOrderVo = this.bo2vo(orderBo);
            adminOrderVOList.add(adminOrderVo);
        }
        return adminOrderVOList;
    }
    public AdminOrderItemVO bo2vo(OrderBO bo) {
        AdminOrderItemVO order = new AdminOrderItemVO();
        if (bo == null) {
            return order;
        }
        BeanUtils.copyProperties(bo, order);
        return order;
    }
    public AdminOrderDBPagerQuery query2dbQuery(AdminOrderListQuery adminOrderListQuery) {
        if (adminOrderListQuery == null) {
            return new AdminOrderDBPagerQuery();
        }
        AdminOrderDBPagerQuery dbQuery = new AdminOrderDBPagerQuery();
        BeanUtils.copyProperties(adminOrderListQuery, dbQuery);
        System.out.println(dbQuery.getLimit());
        return dbQuery;
    }
    public List<OrderBO> dos2bos(List<OrderDO> list) {
        List<OrderBO> orderBos = new ArrayList<>(list.size());
        for (OrderDO order : list) {
            orderBos.add(this.do2bo(order));
        }
        return orderBos;
    }
    public OrderBO do2bo(OrderDO order) {
        OrderBO orderBO = new OrderBO();
        if (order == null) {
            return new OrderBO();
        }
        BeanUtils.copyProperties(order, orderBO);
        return orderBO;
    }
    public AdminOrderDetailVO bo2detailVo(OrderBO bo) {
        AdminOrderDetailVO order = new AdminOrderDetailVO();
        //订单信息
        order.setOrderInfo(bo2OrderInfoVo(bo));
        //需求单信息
        order.setRequestOrderInfo(bo2RequestOrderInfoVo(bo));
        //附件信息
        order.setAttachInfo(bos2AttachInfoVos(bo.getAttachInfoBO()));
        //用户信息
        order.setUserInfo(bo2UserInfoVo(bo));
        //师傅信息
        order.setWorkerInfo(bo2WorkerInfoVo(bo));
        return order;
    }
    public AdminOrderInfoVo bo2OrderInfoVo(OrderBO bo) {
        AdminOrderInfoVo info = new AdminOrderInfoVo();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }
    public AdminRequestOrderInfoVO bo2RequestOrderInfoVo(OrderBO bo) {
        AdminRequestOrderInfoVO info = new AdminRequestOrderInfoVO();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }
    public List<AttachInfoVO> bos2AttachInfoVos(List<AttachInfoBO> attachInfoBOList) {
        if (CollectionUtils.isEmpty(attachInfoBOList)) {
            return Collections.emptyList();
        }
        List<AttachInfoVO> orderVOList = new ArrayList<>(attachInfoBOList.size());
        for (AttachInfoBO attachInfoBO : attachInfoBOList) {
            AttachInfoVO orderVo = this.bo2vo(attachInfoBO);
            orderVOList.add(orderVo);
        }
        return orderVOList;
    }
    public AttachInfoVO bo2vo(AttachInfoBO bo) {
        AttachInfoVO info = new AttachInfoVO();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }
    public AdminUserInfoVO bo2UserInfoVo(OrderBO bo) {
        AdminUserInfoVO info = new AdminUserInfoVO();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }
    public AdminWorkerInfoVO bo2WorkerInfoVo(OrderBO bo) {
        AdminWorkerInfoVO info = new AdminWorkerInfoVO();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }

    public OrderDO param2do(OrderCreateParam param) {
        OrderDO order = new OrderDO();
        if (param == null) {
            return order;
        }
        BeanUtils.copyProperties(param, order);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        order.setGmtCreate(System.currentTimeMillis());
        order.setGmtModified(order.getGmtCreate());
        order.setCreateUserId(loginUser != null ? loginUser.getId() : 0);
        order.setModifiedUserId(loginUser != null ? loginUser.getId() : 0);
        order.setCreateUserName(loginUser != null ? loginUser.getUsername() : "mock");
        order.setModifiedUserName(loginUser != null ? loginUser.getUsername() : "mock");
        return order;
    }

    public OrderDO param2do(OrderParam param) {
        OrderDO order = new OrderDO();
        if (param == null) {
            return order;
        }
        BeanUtils.copyProperties(param, order);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        order.setGmtCreate(System.currentTimeMillis());
        order.setGmtModified(order.getGmtCreate());
        order.setCreateUserId(loginUser != null ? loginUser.getId() : 0);
        order.setModifiedUserId(loginUser != null ? loginUser.getId() : 0);
        order.setCreateUserName(loginUser != null ? loginUser.getUsername() : "mock");
        order.setModifiedUserName(loginUser != null ? loginUser.getUsername() : "mock");
        return order;
    }

    public PagerResult<FrontOrderItemVO> assemblePagerResult(
        ListRecordTotalBO<OrderBO> bos, SimplePagerQuery query) {
        List<FrontOrderItemVO> orderVOList = this.bos2itemvos(bos.getList());
        PagerResult<FrontOrderItemVO> pagerResult = new PagerResult<>(query);
        pagerResult.setObjects(orderVOList);
        pagerResult.setTotal(bos.getTotal());
        return pagerResult;
    }
    public List<FrontOrderItemVO> bos2itemvos(List<OrderBO> orderBOS){
        if (CollectionUtils.isEmpty(orderBOS)) {
            return Collections.emptyList();
        }
        List<FrontOrderItemVO> orderVOList = new ArrayList<>(orderBOS.size());
        for (OrderBO orderBo : orderBOS) {
            FrontOrderItemVO orderVo = this.bo2itemvo(orderBo);
            orderVOList.add(orderVo);
        }
        return orderVOList;
    }

    private FrontOrderItemVO bo2itemvo(OrderBO bo) {
        FrontOrderItemVO order = new FrontOrderItemVO();
        if (bo == null) {
            return order;
        }
        BeanUtils.copyProperties(bo, order);
        return order;
    }

    public FrontOrderDBPagerQuery query2dbQuery(FrontOrderListQuery query) {
        if (query == null) {
            return new FrontOrderDBPagerQuery();
        }
        FrontOrderDBPagerQuery orderDBPagerQuery = new FrontOrderDBPagerQuery();
        BeanUtils.copyProperties(query, orderDBPagerQuery);
        return orderDBPagerQuery;
    }

    public FrontOrderDetailVO bo2frontDetailVO(OrderBO bo) {
        FrontOrderDetailVO order = new FrontOrderDetailVO();
        //订单信息
        order.setOrderInfo(bo2frontOrderInfoVO(bo));
        //需求单信息
        order.setRequestOrderInfo(bo2frontRequestOrderInfoVO(bo));
        //附件信息
        order.setAttachInfo(bos2AttachInfoVos(bo.getAttachInfoBO()));
        //用户信息
        order.setUserInfo(bo2FrontUserInfoVO(bo));
        return order;
    }

    private FrontUserInfoVO bo2FrontUserInfoVO(OrderBO bo) {
        FrontUserInfoVO info = new FrontUserInfoVO();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }

    private FrontRequestOrderInfoVO bo2frontRequestOrderInfoVO(OrderBO bo) {
        FrontRequestOrderInfoVO info = new FrontRequestOrderInfoVO();
        if (bo == null) {
            return info;
        }
        BeanUtils.copyProperties(bo, info);
        return info;
    }

    private FrontOrderInfoVo bo2frontOrderInfoVO(OrderBO bo) {
        FrontOrderInfoVo order = new FrontOrderInfoVo();
        if (bo == null) {
            return order;
        }
        BeanUtils.copyProperties(bo, order);
        order.setCreateTime(bo.getGmtCreate());
        return order;
    }
}
